package com.youlai.boot.modules.content.controller;


import com.youlai.boot.modules.content.model.form.LzCategoryForm;
import com.youlai.boot.modules.content.model.query.LzCategoryQuery;
import com.youlai.boot.modules.content.model.vo.LzCategoryVO;
import com.youlai.boot.modules.content.service.LzCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.ArrayList;

/**
 * 分类前端控制层
 *
 * @author youlaitech
 * @since 2025-02-20 11:07
 */
@Tag(name = "分类接口")
@RestController
@RequestMapping("/api/v1/lzCategorys")
@RequiredArgsConstructor
public class LzCategoryController {

    private final LzCategoryService lzCategoryService;

    @Operation(summary = "分类分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('content:lzCategory:query')")
    public PageResult<LzCategoryVO> getLzCategoryPage(LzCategoryQuery queryParams) {
        IPage<LzCategoryVO> result = lzCategoryService.getLzCategoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('content:lzCategory:add')")
    public Result<Void> saveLzCategory(@RequestBody @Valid LzCategoryForm formData) {
        boolean result = lzCategoryService.saveLzCategory(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取分类表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('content:lzCategory:edit')")
    public Result<LzCategoryForm> getLzCategoryForm(
            @Parameter(description = "分类ID") @PathVariable Long id
    ) {
        LzCategoryForm formData = lzCategoryService.getLzCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改分类")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('content:lzCategory:edit')")
    public Result<Void> updateLzCategory(
            @Parameter(description = "分类ID") @PathVariable Long id,
            @RequestBody @Validated LzCategoryForm formData
    ) {
        boolean result = lzCategoryService.updateLzCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('content:lzCategory:delete')")
    public Result<Void> deleteLzCategorys(
            @Parameter(description = "分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lzCategoryService.deleteLzCategorys(ids);
        return Result.judge(result);
    }

    // 根据id 修改状态
    @Operation(summary = "修改分类状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPerm('content:lzCategory:edit')")
    public Result<Void> updateLzCategoryStatus(
            @Parameter(description = "分类ID") @PathVariable Long id,
            @Parameter(description = "状态(0:正常,1禁用)") @RequestParam Integer status
    ) {
        boolean result = lzCategoryService.updateLzCategoryStatus(id, status);
        return Result.judge(result);
    }

    // 获取所有分类
    @Operation(summary = "获取所有分类")
    @GetMapping("/all")
    public Result<ArrayList<LzCategoryVO>> getAllLzCategorys() {
        return Result.success(lzCategoryService.getAllLzCategorys());
    }

}
